package com.example.rubbish.Utils;

import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class okhttpUtils {
    private static final String ADDRESS_TEXT="https://aiapi.jd.com/jdai/garbageTextSearch";
    private static final String ADDRESS_IMAGE="https://aiapi.jd.com/jdai/garbageImageSearch";
//    private static final String ADDRESS_VOICE="https://aiapi.jd.com/jdai/garbageVoiceSearch";
    private static final String ADDRESS_Hot="https://api.tianapi.com/hotlajifenlei/index";


    private static final String STANDARD_SHANGHAI="310000";
    private static final MediaType MEDIA_TYPE_JSON =MediaType.parse("application/json; charset=utf-8");
    //    private static final MediaType MEDIA_TYPE_JSON =MediaType.Companion.parse("application/json; charset=utf-8");

    public static void TextSearchRequest( String content, okhttp3.Callback callback) {
        long timeStamp =keyHelper.getTimestamp();
        String appKey =keyHelper.getAppKey();
        String sign = keyHelper.getSign(timeStamp);
//        HttpUrl httpUrl = new HttpUrl.Builder()
//                .scheme("http")
//                .host("aiapi.jd.com")
//                .addPathSegment("jdai")
//                .addPathSegment("garbageTextSearch")
//                .addQueryParameter("appkey",appkey)
//                .addQueryParameter("timestamp", String.valueOf(timeStamp))
//                .addQueryParameter("sign",sign)
//                .build();
        /**这个不行
         //        String httpUrl =ADDRESS_TEXT
         //                +"?"
         //                +"appkey="
         //                +appkey
         //                +"&timestamp="
         //                +timeStamp
         //                +"&sign="
         //                +sign;
         */
        //1.okhttpClient对象
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                //设置超时时间
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20,TimeUnit.SECONDS)
                //错误重连
                .retryOnConnectionFailure(true)
                .build();

        String data="{text:\"" + content + "\",cityId:\"" + "310000" + "\"}";
        RequestBody body = RequestBody.create(MEDIA_TYPE_JSON,data);
        Request request = new Request.Builder()
                .url(ADDRESS_TEXT+"?appkey="+appKey+"&timestamp="+timeStamp+"&sign="+sign)
                .addHeader("Content-Type","application/json;charset=UTF-8")
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);

    }

    public static void hotNewsRequest(okhttp3.Callback callback){
        String key=keyHelper.getHotKey();
        OkHttpClient client = new OkHttpClient.Builder()
//                //在这里，还可以设置数据缓存等
//                .connectTimeout(20, TimeUnit.SECONDS)
//                //设置超时时间
//                .readTimeout(20, TimeUnit.SECONDS)
//                .writeTimeout(20,TimeUnit.SECONDS)
//                //错误重连
//                .retryOnConnectionFailure(true)
                .build();
        Request request = new Request.Builder()
                .url(ADDRESS_Hot+"?key="+key)
                .build();
        client.newCall(request).enqueue(callback);

    }

    public static  void  PhotoSearchRequest(String bitMap ,okhttp3.Callback callback) {
        long timeStamp =keyHelper.getTimestamp();
        String appKey =keyHelper.getAppKey();
        String sign = keyHelper.getSign(timeStamp);

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                //设置超时时间
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20,TimeUnit.SECONDS)
                //错误重连
                .retryOnConnectionFailure(true)
                .build();

        String data="{imgBase64:\"" + bitMap + "\",cityId:\"" + "310000" + "\"}";
        RequestBody body = RequestBody.create(MEDIA_TYPE_JSON,data);
        Request request = new Request.Builder()
                .url(ADDRESS_IMAGE+"?appkey="+appKey+"&timestamp="+timeStamp+"&sign="+sign)
                .addHeader("Content-Type","application/json;charset=UTF-8")
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);


    }
}
